<template>
    <div>
         <!-- 面包屑导航区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 卡片视图区域 -->
        <el-card>
              <!-- 添加分类按钮 -->
            <el-row>
                <el-col>
                    <el-button type="primary" @click="showAddCateDialog" style="margin-bottom:20px">添加分类</el-button>
                </el-col>
            </el-row>
              <!-- 主体内容区域 -->
            <tree-table :data="cateList" :columns="columns" stripe border
            :selection-type="false" :expand-type="false" show-index index-text="#">
            <!-- 是否有效 -->
            <template slot="isok" slot-scope="scope" >               
                <i class="el-icon-success" v-if="scope.row.cat_deleted === false"
                style="color:lightgreen"></i>
                <i class="el-icon-error" v-else style="color:red"></i>
            </template>
            <!-- 排序 -->
            <template slot="sort" slot-scope="scope">
                <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
                <el-tag type="success" size="mini" v-else-if="scope.row.cat_level === 1">二级</el-tag>
                <el-tag type="warning" size="mini" v-else-if="scope.row.cat_level === 2">三级</el-tag>
             <!-- 操作 -->
            </template>
                
            <template slot="operate" slot-scope="scope">
                <el-button  type="primary" size="mini"  @click="editCate(scope.row.cat_id)">
                    <i class="el-icon-edit" style="margin-right:5px"></i>编辑
                </el-button>
                <el-button  type="danger" size="mini"  @click="deleteCate(scope.row.cat_id)">
                    <i class="el-icon-delete" style="margin-right:5px"></i>删除
                </el-button>
            </template>
            </tree-table>
             
             <!-- //底部分页栏 -->
              <el-pagination @size-change="handleSizeChange"  @current-change="handleCurrentChange" 
              :current-page="querInfo.pagenum" :page-sizes="[3, 5, 10, 15]"
              :page-size="querInfo.pagesize" :total="total" layout="total, sizes, prev, pager, next, jumper">
            </el-pagination>

            
            <!-- 添加分类的对话框 -->
            <el-dialog title="添加分类" :visible.sync="addCateDialogVisible" width="50%">
                <el-form :model="addCateForm" ref="addCateFormRef" 
                :rules="addCateFormRules" label-width="100px" >
                    <el-form-item label="分类名称:"  prop="cat_name">
                        <el-input v-model="addCateForm.cat_name" ></el-input>
                </el-form-item>
                    <el-form-item label="父级分类">
                        <el-select v-model="addCateForm.cat_name" placeholder="请选择">
                        <el-option label="大家电" value="shanghai"></el-option>
                        <el-option label="区域二" value="beijing"></el-option>
                        </el-select>
                </el-form-item>
                <span slot="footer" class="dialog-footer">
                    <el-button @click="addCateDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="addCateDialogVisible = false">确 定</el-button>
                </span>
                </el-form>
            </el-dialog>

            <!-- 编辑分类的对话框 -->
            <el-dialog title="编辑分类" :visible.sync="editCateDialogVisible">
            <el-form :model="editCateForm" ref="editCateFormRef" label-width="70px">
                <el-form-item label="分类名称" prop="editCateForm">
                    <el-input v-model="editCateForm.cat_name"></el-input>
                </el-form-item>
            </el-form>  
            </el-dialog>
        </el-card>
      
    </div>
</template>

<script>
export default {
   data(){
       return{
           //查询条件
        querInfo:{
               type:3,
               pagenum: 1,
               pagesize: 5
           },
        // 获取的商品分类列表
        cateList:[] ,
          //总数据条数
        total:0,
        columns:[
            {
                label:'分类名称',
                prop:'cat_name'
            },
            {
                label:'是否有效',
                //prop:'cat_deleted',
                //将当前列定义为模板列
                type:'template',
                template:'isok'
            },
            {
                label:'排序',
               // prop:'cat_level'
                type:'template',
                template:'sort'
            },
             {
                label:'操作',
                type:'template',
                template:'operate'
            }
          ],
        addCateDialogVisible:false,
        //添加分类的表单数据对象
        addCateForm:{
            //将要添加的分类的名称
            cat_name:'',
            //父级分类的id
            cat_pid:0,
            cat_level:0
        },
        //添加分类的校验规则
        addCateFormRules:{
             cat_name:[
                {required:true, message:"请输入分类名称", trigger:'blur'} 
             ]
        },
        // 编辑分类对话框的显示与隐藏
        editCateDialogVisible:false,
       //编辑分类的表单数据对象
       editCateForm:{
           cat_name:'',
           cat_pid:'',
           cat_level:'',
       }
       }
   },
    created(){
        this.getCateList()
    },
    methods:{
        //获取商品分类数据
        async getCateList(){
            const {data : res} = await this.$http.get('categories',{params:this.querInfo})
            if(res.meta.status !== 200){
                return this.$message.error("获取商品分类数据失败");
            }
            this.$message.success("获取商品分类数据成功")
           //console.log(res);
            this.cateList = res.data.result
            //为总数据条数赋值
            this.total = res.data.total
        },
         //监听pagesize的改变事件
        handleSizeChange(newSize){
            this.querInfo.pagesize = newSize
            this.getCateList()
         },
         //监听页码值pagenum的事件
        handleCurrentChange(newNum){
            this.querInfo.pagenum = newNum
            this.getCateList()

        },
         
        // 添加分类对话框的显示与隐藏
        showAddCateDialog(){
            this.addCateDialogVisible = true
        },
        //点击删除按钮，删除分类
        async deleteCate(id){
           // console.log(id)
            const confirmResult =  await this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', 
            {confirmButtonText: '确定', cancelButtonText: '取消',type: 'warning'
            }).catch(err => err)
            //如果用户确认删除，返回cancle
            //如果用户确认删除，返回confirm
            if(confirmResult !== 'confirm'){
                return this.$message.info('已取消删除')
                }
            const {data : res} = await this.$http.delete('categories/'+id)
            //console.log(res);
            if (res.meta.status !== 200){
                this.$message.error("删除分类失败")
            }
            this.$message.success("删除分类成功")
            this.getCateList()
        },

        //点击编辑按钮，编辑分类
        editCate(id){
        console.log(id);
        this.editCateDialogVisible = true
            
        }
       

       

    }
}
</script>

<style lang="less" scoped>

</style>